package com.briup.web.listener;

import com.briup.bean.Book;
import com.briup.bean.Category;
import com.briup.service.IBookService;
import com.briup.service.ICategoryService;
import com.briup.service.impl.IBookServiceImpl;
import com.briup.service.impl.ICategoryServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class ApplicationListner implements ServletContextListener {

    private ICategoryService categoryService = new ICategoryServiceImpl();
    private IBookService bookService=new IBookServiceImpl();

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //查询一级分类和二级分类
        //1.调用 service 层
        List<Category> list = categoryService.findAllCategorys();
        //2.放到 application 对象中
        ServletContext application = sce.getServletContext();
        application.setAttribute("categories",list);

        //查询所有书籍
        //1.调用 service 层
        List<Book> books = bookService.findAllBooks();
        //2.放到 application 对象中
        application.setAttribute("books",books);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContextListener.super.contextDestroyed(sce);
    }
}